<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
		<!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
		<meta name="renderer" content="webkit">
		<!--国产浏览器高速模式-->
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>电力通信管理系统</title>

		<!-- 公共样式 开始 -->
		<link rel="stylesheet" type="text/css" href="../../css/base.css">
		<link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
		<script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<!-- 滚动条插件 -->
		<link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
		<script src="../../framework/jquery-ui-1.10.4.min.js"></script>
		<script src="../../framework/jquery.mousewheel.min.js"></script>
		<script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
		<script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
		<!-- 公共样式 结束 -->
		<script type="text/javascript" src="../../framework/frame.js" ></script>
		<style>
			.layui-form{
				margin-right: 30%;
			}
		</style>

	</head>

	<body>
		<div class="cBody">
				<form id="form" action="" method="post" class="layui-form">
					<div class="layui-form-item">
						<label class="layui-form-label">光缆名称</label>
						<div class="layui-input-inline shortInput">
							<input  type="text" id="cablesname" name="cablesname" required lay-verify="cablesname" placeholder="请输入光缆名称" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">起始段</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablesstart" name="cablesstart" required lay-verify="cablesstart" placeholder="请输入光缆起始段" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">终止段</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablesdestination" name="cablesdestination" required lay-verify="cablesdestination" placeholder="请输入光缆终止段" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">总芯数</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablessum" name="cablessum" required lay-verify="cablessum" placeholder="请输入总芯数" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">已用芯数</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablesused" name="cablesused" required lay-verify="cablesused" placeholder="请输入已用芯数" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">闲置芯数</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablesfree" name="cablesfree" required lay-verify="cablesfree" placeholder="请输入闲置芯数" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">节点个数</label>
						<div class="layui-input-block ">
							<input  type="text" id="cablesnumber" name="cablesnumber" required lay-verify="cablesnumber" placeholder="请输入节点个数" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" id="btn" lay-submit lay-filter="submitBut">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>
		</div>
	</body>

<script>
	$(document).ready(function(){
	});
	layui.use('form', function() {
		var form = layui.form;
		form.verify({
			cablesname: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入光缆名称！';
				}
			},
			cablesstart: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入起始段！';
				}
			},
			cablesdestination: function(value, item){
				if(value == null || value == ""){
					return '请输入终止段！';
				}
			},
			cablessum: function(value, item){
				if(value == null || value == ""){
					return '请输入总芯数！';
				}
			},
			cablesused: function(value, item){
				if(value == null || value == ""){
					return '请输入已用芯数！';
				}
			},
			cablesfree: function(value, item){
				if(value == null || value == ""){
					return '请输入闲置芯数！';
				}
			},
			cablesnumber: function(value, item){
				if(value == null || value == ""){
					return '请输入节点个数！';
				}
			}
		});
		//监听提交
		form.on('submit(submitBut)', function(data) {
			var body=data.field;
			if((body.cablesused*1)+(body.cablesfree*1)!==(body.cablessum*1)){
				console.log(body)
				layer.msg("总芯数必须等于已用芯数加上闲置芯数")
				return  false
			}
			console.log(body)
			$.ajax({
				type: "post",
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				dataType: "json",
				url: "http://localhost:8888/cable/save",
				contentType: "application/json;charset=UTF-8",//指定消息请求类型
				data: JSON.stringify(body),//将js对象转成json对象
				success: function (data,status,xhr) {
					if (data.code === 0) {
						layer.msg("添加成功");
						$("#form")[0].reset()
						form.render()
					} else {
						layer.msg(data.msg)
					}
					return false
				},
				error:function () {
					layer.msg("添加失败，请稍后再试")
					return false
				}
			})
            return  false
		});
	});

</script>

</html>
